<?php

namespace App\Http\Controllers;

use App\Parts;
use App\Product;
use App\ProductParts;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Repositories\ProductRepository;


class ProductController extends Controller
{
    protected $productRepository;

    public function __construct(ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
        view()->share('path', 'product');
    }

    public function index()
    {
        $list = $this->productRepository->pageList([],['*'],10, $sortBy = 'id', $sort = 'ASC');

        return view('product.index', compact('list'));
    }

    public function store(Request $request, $id = '')
    {
        $data = $request->only(['name', 'partsname']);

        $parts_name = explode("，", $data['partsname']);

        $data1 = [
            'name' => $data['name'],
        ];

        try {
            DB::beginTransaction();
            if ($id) {
                Product::whereId($id)->update($data1);

            } else {

                 Product::create($data1);


            }
        } catch (\Exception $e) {
            dd($e->getMessage());
            DB::rollback();
            return redirect()->back()->with('rs', ['flag' => 'error', 'msg' => '编辑失败']);
        }

        DB::commit();
        return redirect('product')->with('rs', ['flag' => 'success', 'msg' => '编辑成功']);
    }


    public function getInfo($id)
        {
            try {
                $oneProduct=Product::where('id',$id)->first();
                return response()->json(['rs' => 'true', 'data' => $oneProduct]);
            } catch (\Exception $e) {;
                return response()->json(['rs' => 'false', 'msg' => '错误的车型信息']);
            }
        }

    public function doDelete(Request $request)
    {
        try {
            $id = $request->get('_id');
            // 如果不存在ID,则返回错误
            if (!$id) {
                return response()->json(['rs' => 'false', 'msg' => '错误信息']);
            }

            $ids = explode(',', $id);
            // 使用软删除删除数据
            Product::whereIn('id', $ids)->delete();

            return response()->json(['rs' => 'true', 'msg' => '操作成功']);
        } catch (\Exception $e) {;
            return response()->json(['rs' => 'false', 'msg' => '请刷新重新尝试']);
        }
    }

    /**
     * 跳转到添加配件页面
     * @author wxh
     */
    public function addparts($product_id)
    {
        $list   = Parts::pluck('name','id');
        $parts  = ProductParts::where('product_id',$product_id)->get();
        return view('product.addparts',compact('list','product_id','parts'));
    }

    public function deleteparts(Request $request)
    {
        try {
            $id = $request->get('_id');
            // 如果不存在ID,则返回错误
            if (!$id) {
                return response()->json(['rs' => 'false', 'msg' => '错误信息']);
            }

            $ids = explode(',', $id);
            ProductParts::whereIn('id', $ids)->delete();
            return response()->json(['rs' => 'true', 'msg' => '操作成功']);
        } catch (\Exception $e) {;
            return response()->json(['rs' => 'false', 'msg' => '请刷新重新尝试']);
        }
    }

    public function setparts(Request $request)
    {
        $data=$request->except('_token');
        ProductParts::where('product_id',$data['product_id'])->delete();
        for ($i=0;$i<count($data['part_id']);$i++){
            $data1=[
                'product_id'=>$data['product_id'],
                'parts_id' => $data['part_id'][$i],
                'parts_num' => $data['num'][$i],
            ];
            ProductParts::create($data1);
        }
        return redirect('product')->with('rs', ['flag' => 'success', 'msg' => '编辑成功']);
    }

}
